function reloadProductList() {
    var products;
    $.ajax({
        type: 'POST',
        url: webroot + 'products/ajaxSearch/',
        success: function(message) {
            products = jQuery.parseJSON(message);
            drawSearchResult(products);
        }
    });
}

function drawPagination(current_page, total_page) {
    $('#search_pagination').empty();

    var first = Math.max(1, current_page - 3);
    var last = Math.min(current_page + 3, total_page);
    for(var i = first; i <= last; ++i) {
        $('#search_pagination').append('<span'
            + ((i == current_page) ? ' class="current"' : '')
            + ' id="page_' + i + '">'
            + ((i == current_page) ? i : '<a href="javascript:void(0)">' + i + '</a>')
            + '</span>&nbsp;'
        );

        if (i != current_page) {
            $('#page_' + i).click(function(event){
                var id = event.target.parentNode.id;
                id = id.substring(id.indexOf('_') + 1, id.length);
                $.ajax({
                    type: 'POST',
                    url: webroot + 'products/ajaxSearchOption/page/' + id,
                    success: function(message) {
                        $.jGrowl(message);
                        reloadProductList();
                    }
                });
            });
        }
    }
}

function drawSearchResult(products) {
    drawPagination(products[0].current_page, products[0].total_page);
    $('#search_result').empty().css('margin-top', '50px');
    for(var i = 0; i < products.length; ++i) {
        var product_url = webroot + 'products/view/' + products[i].Product.id;
        var deal_url = webroot + 'deals/product_deals/' + products[i].Product.id;
        var img_url = webroot + 'img/posts/' + products[i].Product.image;
        if (null == products[i].Product.short_description)
            products[i].Product.short_description = '';

        if (products[i].Product.name.length > 60)
            products[i].Product.name = products[i].Product.name.substring(0, 57) + '...';

        $('#search_result').append('<div class="catrow">'
                + '<div class="rowleft"><a href="' + product_url + '"><img src="' + img_url + '" style="width:200px;height:180px;margin:20px 0px"/></a></div>'
                + '<div class="rowright">'
                    + '<div class="ptitle"><span>' + products[i].Product.name + '</span></div>'
                    + '<div class="pinfo"><ul class="pilt">'
                        + '<li>Manufacturer: <a href="' + products[i].Manufacturer.url + '">' + products[i].Manufacturer.name + '</a></li>'
                        + '<li>Category: ' + products[i].Category.name + '</li>'
                        + '<li style="height:100px;margin-top:5px" class="scrollItem">' + products[i].Product.description + '</li>'
                    + '</ul></div>'
                + '</div>'
            + '</div>'
        );
    }
    $('.scrollItem').mCustomScrollbar();
}

$(document).ready(function() {
    reloadProductList();
    $('.category_selector').click(function(event){
        if ($(event.target.parentNode).hasClass('selected')) {
            $(event.target.parentNode).removeClass('selected');
            $.ajax({
                type: 'POST',
                url: webroot + 'products/ajaxSearchOption/remove_category/' + event.target.id,
                success: function(message) {
                    $.jGrowl(message);
                    reloadProductList();
                }
            });
        } else {
            $(event.target.parentNode).addClass('selected');
            $.ajax({
                type: 'POST',
                url: webroot + 'products/ajaxSearchOption/category/' + event.target.id,
                success: function(message) {
                    $.jGrowl(message);
                    reloadProductList();
                }
            });
        }
    });

    $('.manufacturer_selector').click(function(event){
        if ($(event.target.parentNode).hasClass('selected')) {
            $(event.target.parentNode).removeClass('selected');
            $.ajax({
                type: 'POST',
                url: webroot + 'products/ajaxSearchOption/remove_manufacturer/' + event.target.id,
                success: function(message) {
                    $.jGrowl(message);
                    reloadProductList();
                }
            });
        } else {
            $(event.target.parentNode).addClass('selected');
            $.ajax({
                type: 'POST',
                url: webroot + 'products/ajaxSearchOption/manufacturer/' + event.target.id,
                success: function(message) {
                    // $.jGrowl(message);
                    reloadProductList();
                }
            });
        }
    });
});
